package com.wwu.system.controller;

import com.wwu.common.constant.CommConst;
import com.wwu.common.enums.MenuBtnEnum;
import com.wwu.common.web.ResultObj;
import com.wwu.common.web.ResultPage;
import com.wwu.system.common.aspect.Log;
import com.wwu.system.entity.SysDict;
import com.wwu.system.service.ISysDictService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 *  系统字典前端控制器
 * @author 一蓑烟雨
 * @date 2021-10-26
 * @version 1.0.0
 */
@Api(tags = "系统字典控制器(SysDictController)")
@ApiSort(9)
@Controller
@RequestMapping("/system/sysDict")
public class SysDictController {
    @Resource
    private ISysDictService sysDictService;

    /**
     * 请求转发字典类型增加/编辑主页面
     * @param dictTypeId  字典类型编码
     * @param dictId    字典编码
     * @param model
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/7/6 16:47
     */
    @GetMapping("/dictAddOrUpdate")
    public String dictTypeAddOrUpdateHtml(String dictTypeId, String dictId,Model model) {
        if(StringUtils.hasText(dictTypeId) && StringUtils.hasText(dictId)){
            model.addAttribute(CommConst.SYS_DICT,sysDictService.getDictById(dictTypeId,dictId));
        }else{
            model.addAttribute(CommConst.SYS_DICT,new SysDict().setDictTypeId(dictTypeId));
        }
        return "system/sysdict/dict-add-edit";
    }

    @Log(menuName = "字典管理",menuBtnEnum = MenuBtnEnum.SHOW)
    @ApiOperation(value = "分页查询字典信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数",paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数",paramType = "query", required = true),
            @ApiImplicitParam(name = "sysDict", value = "字典实体")
    })
    @GetMapping("/dictList")
    @ResponseBody
    public ResultPage<SysDict> dictList(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "5000") Integer limit,
                                        SysDict sysDict){
        return sysDictService.getDictListPage(page,limit,sysDict);
    }

    @Log(menuName = "字典管理",menuBtnEnum = MenuBtnEnum.ADD)
    @ApiOperation(value = "添加字典")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveDict")
    @ResponseBody
    public ResultObj saveDict(@RequestBody SysDict sysDict){
        sysDictService.saveDict(sysDict);
        return ResultObj.success("字典添加成功");
    }

    @Log(menuName = "字典管理",menuBtnEnum = MenuBtnEnum.EDIT)
    @ApiOperation(value = "更新字典")
    @ApiOperationSupport(order = 3)
    @PutMapping("/updateDict")
    @ResponseBody
    public ResultObj updateDict(@RequestBody SysDict sysDict){
        sysDictService.updateDict(sysDict);
        return ResultObj.success("字典更新成功");
    }

    @Log(menuName = "字典管理",menuBtnEnum = MenuBtnEnum.STATUS)
    @ApiOperation(value = "更新字典状态")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictId",value = "字典编码",required = true),
            @ApiImplicitParam(name = "dictStatus",value = "字典状态(0:启用;1:作废)",required = true)
    })
    @PutMapping("/updateDictStatus")
    @ResponseBody
    public ResultObj updateDictStatus(String dictTypeId, String dictId,Integer dictStatus) {
        sysDictService.updateDictStatus(dictTypeId,dictId,dictStatus);
        return ResultObj.success("字典状态更新成功");
    }

    @Log(menuName = "字典管理",menuBtnEnum = MenuBtnEnum.DEL)
    @ApiOperation(value = "删除字典")
    @ApiOperationSupport(order = 5)
    @DeleteMapping("/deleteDict/{dictTypeId}/{dictId}")
    @ResponseBody
    public ResultObj deleteDict(@ApiParam(name = "dictTypeId",value = "字典类型id",required = true) @PathVariable String dictTypeId,
                                @ApiParam(name = "dictId",value = "字典id")  @PathVariable String dictId){
        sysDictService.deleteDict(dictTypeId,dictId);
        return ResultObj.success("字典删除成功");
    }
}
